package com.shanghaionstar.mro;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.GaodeType;
import com.shanghaionstar.mro.adapter.MroViolationAdapter;
import com.shanghaionstar.mro.bean.violation.Item;
import com.shanghaionstar.mro.bean.violation.ViolationBean;
import com.shanghaionstar.utils.FinalConfigParams;

import java.util.List;

public class MroViolationFragment extends MrOBaseFragment {
    private View view;
    @FindViewById
    private ListView breakrule_list;
    private List<Item> items;
    private MroViolationAdapter adapter;
    @FindViewById
    private TextView tv_violation_num, tv_right;
    @FindViewById
    private RelativeLayout rl_violation_info;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.mro_fragment_violation, container, false);
            injectView(view);
        } else {
            super.onResumeView(view);
        }
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Bundle bundle = getArguments();
        String plateNum = bundle.getString(FinalConfigParams.LICENSE_PLATE_NUM);
        if (!CommonUtil.isNull(bundle)) {
            String violations = bundle.getString("violations");
            String code = bundle.getString("code");
            ViolationBean violationBean = (ViolationBean) bundle.getSerializable(GaodeType.Violation.value());
            if (!CommonUtil.isNull(code)) {
                if (code.equals("1")) {
                    if (!CommonUtil.isNull(violationBean)&&violationBean.getList()!=null&&violationBean.getList().size()<=0) {
                        tv_right.setText(getString(R.string.SB008_MSG006, plateNum));
                        rl_violation_info.setVisibility(View.GONE);
                    } else {
                        if (!CommonUtil.isNull(violationBean)) {
                            if (!CommonUtil.isNull(violationBean.getList())) {
                                if (!CommonUtil.isNull(plateNum)) {
                                    tv_right.setText(getString(R.string.SB008_MSG005, plateNum));
                                } else {
                                    tv_right.setText(getString(R.string.SB008_MSG001));
                                }
                                String violationNum = String.valueOf(violationBean.getList().size());
                                tv_violation_num.setText(getString(R.string.mro_violation_num, violationNum));
                                adapter = new MroViolationAdapter(getActivity(), violationBean.getList());
                                breakrule_list.setAdapter(adapter);
                            } else {
                                tv_right.setText(getString(R.string.SB008_MSG006, plateNum));
                                rl_violation_info.setVisibility(View.GONE);
                            }
                        }
                    }
                } else if (code.equals("1013")) {
                    tv_right.setText(getActivity().getString(R.string.SB008_MSG007));
                    rl_violation_info.setVisibility(View.GONE);
                } else {
                    tv_right.setText(getActivity().getString(R.string.SB008_MSG008));
                    rl_violation_info.setVisibility(View.GONE);
                }
            } else {
                tv_right.setText(getActivity().getString(R.string.SB008_MSG008));
                rl_violation_info.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        view = null;
    }
}
